<template>

  <el-container class="home-container">
    <el-header height="120px">
      <div>
        <img src="../assets/logo.jpg"/>
        <span align="center">鲜花网后台管理系统</span>
      </div>
      <el-dropdown>
        <el-button type="primary" class="mainBtn">
          <span class="larger-font">{{ loginName }}</span><i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>
            <el-link type="info" @click="dialogFormVisible=true">个人信息</el-link>
          </el-dropdown-item>
          <el-dropdown-item>
            <el-link type="info" @click="newDialogFormVisible=true">修改密码</el-link>
          </el-dropdown-item>
          <el-dropdown-item>
            <el-link type="info" @click="logout">退出登录</el-link>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dialog title="个人信息" :visible.sync="dialogFormVisible">
        <el-descriptions class="margin-top" :column="3" :size="size" border :data="accountInfo">
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              用户名
            </template>
            {{ accountInfo.accountName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              用户昵称
            </template>
            {{ accountInfo.nickName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-key"></i>
              用户密码
            </template>
            {{ accountInfo.password }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-mobile-phone"></i>
              手机号
            </template>
            {{ accountInfo.phoneNumber }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-time"></i>
              上次登录时间
            </template>
            {{ accountInfo.loginDate }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-s-promotion"></i>
              电子邮箱
            </template>
            {{ accountInfo.email }}
          </el-descriptions-item>
        </el-descriptions>
      </el-dialog>


      <el-dialog title="密码修改" :visible.sync="newDialogFormVisible">
        <el-form :model="newForm" :rules="rules" ref="newForm" label-width="100px">

          <el-form-item label="新密码" prop="password">
            <el-input v-model="newForm.password"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkPassword">
            <el-input v-model="newForm.checkPassword"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="newDialogFormVisible = false">取 消</el-button>
          <el-button @click="updateResetForm">重 置</el-button>
          <el-button type="primary" @click="newDialogFormVisible = false; toUpdate(newForm);">确 定</el-button>
        </div>
      </el-dialog>




    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleClapse">{{ optionInfo }}</div>
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false">
          <el-submenu :index="item.url" v-for="item in menuList" :key="item.name">
            <template slot="title">
              <i class="el-icon-s-home"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item
                v-if="item.children && item.children.length > 0"
                :index="item.children[0].url"
                :key="item.children[0].name">
              <template slot="title">
                <i class="el-icon-setting"></i>
                <span>{{ item.children[0].name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <!-- 子路由填充位 -->
        <router-view/>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: "MainView",
  data() {
    let checkPassword = (rule, value, callback) => {
      let params1 = this.loginName
      let params2 = this.newForm.password;
      let url = "/account/checkPassword/" + params1 + "/" + params2;
      this.$axios.get(url).then(res => {
        if (res.data.code === 200) {
          callback();
        } else {
          callback(new Error(res.data.message));
        }
      }).catch(error => {
        callback(error);
      });
    };
    return {
      menuList: [],
      isCollapse: false,
      isRouterAlive: true,
      optionInfo: 'close',
      "loginName": '',
      dialogFormVisible: false,
      accountInfo: [],
      formLabelWidth: '120px',
      newDialogFormVisible: false,
      newForm: {
        password: '',
        checkPassword: ''
      },
      rules: {
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'},
          {validator: checkPassword, trigger: 'blur'},
        ],
        checkPassword: [
          {required: true, message: '请再次输入密码', trigger: 'blur'},
          {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'},
          {validator: this.validateCheckPassword, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    validateCheckPassword(rule, value, callback) {
      if (value !== this.newForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },

    toUpdate() {
      this.$refs['newForm'].validate(valid => {
        if (valid) {
          let url = "/account/updateAccountPassword";
          let params = new URLSearchParams;
          params.append('accountName', this.loginName);
          params.append('password', this.newForm.checkPassword);
          this.$axios.put(url, params).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '修改密码成功!'
              });
              this.newDialogFormVisible = false;
              location.reload();
            } else {
              this.$message.error(res.data.message);
            }
          })
        }
      })
    },

    updateResetForm() {
      this.$refs['newForm'].resetFields();
    },

    showLoginName() {
      this.loginName = sessionStorage.getItem("loginName");
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },
    toggleClapse() {
      this.isCollapse = !this.isCollapse;
      if (this.optionInfo == 'close') {
        this.optionInfo = 'open'
      } else {
        this.optionInfo = 'close'
      }
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/");
    },
    loadMenus() {
      let accountName = sessionStorage.getItem("loginName");
      let url = "/menu/getMenuName";
      this.$axios.get(url, {
        params: {
          accountName: accountName
        }
      }).then(res => {
        console.log("------------- 根据用户名得到的菜单 ----------------")
        console.log(res.data);

        if (res.data.code == 200) {
          // 把我们查到的菜单数据，给到 menuList
          this.menuList = res.data.data;
        } else {
          this.$message({
            type: "error",
            message: "菜单加载错误",
            duration: 1000
          })
        }
      })
    },

    getAccountInfo() {
      let url = "/account/findAccountInfo"
      this.$axios.get(url, {
        params: {
          accountName: this.loginName
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.accountInfo = res.data.data;
        } else {
          this.$message({
            type: "error",
            message: "获取用户信息失败",
            duration: 1000
          })
        }
      })
    },
  },
  mounted() {
    this.loadMenus();
    this.showLoginName();
    this.getAccountInfo();
  }
}
</script>

<style lang="less" scoped>

img {
  width: 30px;
  height: 30px;
}

.toggle-button {
  padding: 5px 0;
  background-color: #373d48;
  font-size: 10px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 22px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 14px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}

.aside-gradient {
  background: linear-gradient(to right, #4ca1af, #c4e0e5);
}

.mainBtn {
  --color: #00A97F;
  --color2: rgb(10, 25, 30);
  padding: 0.8em 1.75em;
  background-color: transparent;
  border-radius: 6px;
  border: .3px solid var(--color);
  transition: .5s;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  z-index: 1;
  font-weight: 300;
  font-size: 17px;
  font-family: 'Roboto', 'Segoe UI', sans-serif;
  text-transform: uppercase;
  color: var(--color);
}

.mainBtn::after, .mainBtn::before {
  content: '';
  display: block;
  height: 100%;
  width: 100%;
  transform: skew(90deg) translate(-50%, -50%);
  position: absolute;
  inset: 50%;
  left: 25%;
  z-index: -1;
  transition: .5s ease-out;
  background-color: var(--color);
}

.mainBtn::before {
  top: -50%;
  left: -25%;
  transform: skew(90deg) rotate(180deg) translate(-50%, -50%);
}

.mainBtn:hover::before {
  transform: skew(45deg) rotate(180deg) translate(-50%, -50%);
}

.mainBtn:hover::after {
  transform: skew(45deg) translate(-50%, -50%);
}

.mainBtn:hover {
  color: var(--color2);
}

.mainBtn:active {
  filter: brightness(.7);
  transform: scale(.98);
}

.larger-font {
  font-size: 20px; /* 你可以根据需要调整字体大小 */
}


</style>
